package com.sanji.sjmallmanager.activity;

import android.app.Activity;

import java.util.Stack;

/**
 * Created by fhm on 2016/12/15.
 */

public class ActivityManager {
    private static Stack<Activity> activityStack;
    private static ActivityManager instance;

    private ActivityManager() {
        super();
    }

    /**
     * 单一实例
     */
    public static ActivityManager getInstance() {
        if (instance == null) {
            instance = new ActivityManager();
        }
        return instance;
    }

    /**
     * 添加activity到堆栈中
     */
    public void addActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<>();
        }
        activityStack.add(activity);
    }

    /**
     * 结束当前的activity
     */
    public void finishActivity() {
        Activity activity = activityStack.lastElement();

        if (activity != null) {
            activity.finish();
            activity = null;
        }
    }

    /**
     * 获取当前的activity
     */
    public Activity cureentActivity() {
        Activity activity = activityStack.lastElement();
        return activity;
    }

    /**
     * 结束指定的Activity
     */
    public static void finishActivity(Activity activity) {
        if (activity != null) {
            activity.finish();
            if (activityStack.contains(activity)) {
                activityStack.remove(activity);
            }
            activity = null;
        }
    }

    /**
     * 结束所有的activity
     */
    public static void finishAllActivity() {
        for (int i = 0; i < activityStack.size(); i++) {
            if (null != activityStack.get(i)) {
                activityStack.get(i).finish();
            }
        }
    }

    /**
     * 退出应用程序
     */
    public static void AppExit() {
        finishAllActivity();
        System.exit(0);
    }

}
